#include <string.h>
#include <math.h>
#include <stdio.h>

int maximalRectangle(char **matrix, int matrixSize, int *matrixColSize) {
//    int m = matrixSize;
//    if (m == 0) {
//        return 0;
//    }
//    int n = matrixColSize[0];
//    int left[m][n];
//    memset(left, 0, sizeof(left));
//    for (int i = 0; i < m; i++) {
//        for (int j = 0; j < n; j++) {
//            if (matrix[i][j] == '1') {
//                left[i][j] = (j == 0 ? 0 : left[i][j - 1]) + 1;
//            }
//        }
//    }
    int m = matrixSize;
    if (m == 0) {
        return 0;
    }
    int n = matrixColSize[0];
    int left[m][n];
    memset(left, 0, sizeof(left));
    for (int i = 0; i < m; ++i) {
        for (int j = 0; j < n; ++j) {
            if (matrix[i][j] == '1') {
                left[i][j] = (j == 0 ? 0 : left[i][j - 1] + 1);
            }
        }
    }

//    int ret = 0;
//    for (int i = 0; i < m; i++) {
//        for (int j = 0; j < n; j++) {
//            if (matrix[i][j] == '0') {
//                continue;
//            }
//            int width = left[i][j];
//            int area = width;
//            for (int k = i - 1; k >= 0; k--) {
//                width = fmin(width, left[k][j]);
//                area = fmax(area, (i - k + 1) * width);
//            }
//            ret = fmax(ret, area);
//        }
//    }
//    return ret;
      int ret = 0;
    for (int i = 0; i < m; ++i) {
        for (int j = 0; j < n; ++j) {
            if (matrix[i][j] == '0') {
                continue;
            }
            int width = left[i][j];
            int area = width;
            for (int k = i- 1; k>= 0; k--) {
                width = fmin(width, left[k][j]);
                area = fmax(area, (i - k + 1) * width);
            }
            ret = fmax(ret, area);
        }
    }
    return ret;
}

int main() {
//    [["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]]
    char test[][5] = {{"10100"},
                      {"10111"},
                      {"11111"},
                      {"10010"}};
    char *p[4];
    for (int i = 0; i < 4; ++i) {
        p[i] = test[i];
    }

    int matrixColSize = 5;
    int ret = maximalRectangle(p, 4, &matrixColSize);
    printf(" %d", ret);

}

